package com.guli.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guli.common.handler.EduException;
import com.guli.edu.entity.EduChapter;
import com.guli.edu.entity.EduVideo;
import com.guli.edu.entity.dto.ChapterDto;
import com.guli.edu.entity.dto.VideoDto;
import com.guli.edu.mapper.EduChapterMapper;
import com.guli.edu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.edu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Daniel
 * @since 2019-03-29
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    //根据课程id查询所有的章节 和每个章节里面所有的小姐
    //[{'id':1,'title':'第一章',children:['id',11,'title':'第一节']},{'id':2,'title':'第er章',children:['id',11,'title':'第一节']}]
    @Override
    public List<ChapterDto> getAllChapterVideo(String courseId) {
        // 根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("course_id", courseId);
//        wrapper1.orderByAsc("sort", "id");
        List<EduChapter> eduAllChapters = baseMapper.selectList(wrapper1);

        //查询每个章节里面所有的小节
        QueryWrapper<EduVideo> wrapper2 = new QueryWrapper<>();
        wrapper1.eq("course_id", courseId);
        List<EduVideo> eduAllVideos = eduVideoService.list(wrapper2);

        List<ChapterDto> chapterDtoList = new ArrayList<>();

        for (EduChapter chapter : eduAllChapters) {
            //转换成dto对象
            ChapterDto chapterDto = new ChapterDto();
            BeanUtils.copyProperties(chapter, chapterDto);
            List<VideoDto> videoDtoList = new ArrayList<>();
            for (EduVideo video : eduAllVideos) {
                if (video.getCourseId().equals(chapter.getCourseId()) && chapter.getId() .equals(video.getChapterId())) {
                    VideoDto videoDto = new VideoDto();
                    BeanUtils.copyProperties(video, videoDto);
                    videoDtoList.add(videoDto);
                }
            }
            chapterDto.setChildren(videoDtoList);
            //放到集合里面
            chapterDtoList.add(chapterDto);
        }
        return chapterDtoList;
    }

    @Override
    public boolean removeChapterId(String chapterId) {
        //判断章节下是否有小节
        boolean flag = eduVideoService.getVideoByChapterId(chapterId);
        if (flag) {
            throw new EduException(20001, "章节下面有小节，不能删除");
        }
        int result = baseMapper.deleteById(chapterId);
        return result > 0;
    }

    @Override
    public void removeByCourseId(String id) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",id);
         baseMapper.delete(queryWrapper);
    }
}
